//
//  BKXinLiHuaViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/9.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKXinLiHuaViewController.h"
#import "BKXinLiHuaTableViewCell.h"
#import "BKXinLiHuaViewModel.h"
#import <MBProgressHUD.h>

static NSString *xinlihua = @"xinlihua";
@interface BKXinLiHuaViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation BKXinLiHuaViewController
- (IBAction)p_back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BKXinLiHuaTableViewCell class]) bundle:nil] forCellReuseIdentifier:xinlihua];
    
    __weak BKXinLiHuaViewController *weakSelf = self;
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"正在加载";
    [self.view addSubview:hud];
    [hud removeFromSuperViewOnHide];
    [hud show:YES];
    
    [BKXinLiHuaViewModel xinlihuaWithSuccess:^(NSArray *dataArray) {
        [hud hide:YES];
        weakSelf.dataArray = dataArray;
        [weakSelf performSelectorOnMainThread:@selector(p_reloadData) withObject:nil waitUntilDone:YES];
    } failure:^(NSString *message) {
        [hud hide:YES];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];
    }];
    
    
}
- (void)p_reloadData
{

    [_tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if ([_dataArray isKindOfClass:[NSArray class]]) {
        return [_dataArray count];
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    BKXinLiHuaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:xinlihua];
    if ([_dataArray isKindOfClass:[NSArray class]]  ) {
        if ([_dataArray[indexPath.row] isKindOfClass:[NSDictionary class]]) {
            cell.name.text = _dataArray[indexPath.row][@"ta"];
            cell.time.text = _dataArray[indexPath.row][@"time"];
            cell.content.text = _dataArray[indexPath.row][@"content"];
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([_dataArray isKindOfClass:[NSArray class]]  ) {
    
        if ([_dataArray[indexPath.row] isKindOfClass:[NSDictionary class]]) {
          
            CGSize size =CGSizeMake(tableView.frame.size.width-20,600);

            NSString *text = _dataArray[indexPath.row][@"content"];
            UIFont * tfont = [UIFont systemFontOfSize:15];
             NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
            CGSize  actualsize =[text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
            CGFloat height = actualsize.height+40;
            return height>44?height:60;

        }

    
    }
    
    
    
    

    return 60;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 10;
}


@end
